package com.husawang.husa.lib.base.net;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.husawang.husa.lib.base.utils.L;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by s on 2017/8/9.
 */

public abstract class HusaCallback implements Callback {

	private String TAG = "com.husawang.husa.lib.base.net.HusaCallback";


	public abstract void onHusaReponse(Call call, JSONObject response) throws IOException;

	public abstract void onHusaFailure(String code, String msg);


	@Override
	public void onFailure(Call call, IOException e) {
		L.log(TAG, e);
	}

	@Override
	public void onResponse(Call call, Response response) {

		try {

			JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
			String code = jsonObject.getString("code");

			if ("000000".equals(code)) {

				onHusaReponse(call, jsonObject);

			} else {

				onHusaFailure(code, jsonObject.getString("msg"));

			}


		} catch (IOException e) {
			L.log(TAG, e);
		}

	}
}
